package com.ruoyi.forum.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class EmailService {
    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.properties.mail.from}")
    private String form;

    public void sendEmail(String to,String subject,String text){
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(form);  //设置发送人
        message.setTo(to);      //设置接收人
        message.setSubject(subject);  //设置邮件标题
        message.setText(text);      //设置邮件内容
        javaMailSender.send(message);
    }

    public void sendEmail2(String to,String subject,String text) throws MessagingException {
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom(form);
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://dfs.java.itsource.cn/group1/M00/00/B9/rBE3kWD_fEuALou2AAB4rV4bI7c231.jpg' ><a href='http://localhost:8080/shop/active/22'>点击该链接激活</a>",true);
        //添加附件
//        helper.addAttachment("p7.jpg",new File("C:\\Users\\Administrator\\Pictures\\photos\\p7.jpg"));
        helper.addAttachment("g1.gif", new File("C:\\Users\\OrionStar\\Desktop\\神子枫叶1.jpg"));
        //收件人
        helper.setTo(to);
        javaMailSender.send(mimeMessage);
    }
}
